package com.mask.im.business.service.impl;
import com.mask.im.business.service.ChannelService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChannelServiceImpl implements ChannelService {

	//保存用户id和channel的映射
	public static HashMap<String,Channel> userIdChannel = new HashMap<>();
	//保存channelId和channel映射关系
	public static HashMap<String,Channel> idChannel = new HashMap<>();
	//保存channel和userID映射关系
	public static HashMap<Channel,String> ChannelUserId = new HashMap<>();

	@Override
	public void setUserIdBindChannel(Channel channel) {
		String userId = String.valueOf(MaskIMSecurityUtils.getUserId());
		userIdChannel.put(userId,channel);
	}

	@Override
	public void setIdBindChannel(String id, Channel channel) {
		idChannel.put(id,channel);
	}

	@Override
	public void setChannelBindUserId(Channel channel) {
		String userId = String.valueOf(MaskIMSecurityUtils.getUserId());
		ChannelUserId.put(channel,userId);
	}


	@Override
	public void deleteChannelBindUserId(Channel channel) {
		ChannelUserId.remove(channel);
	}

	@Override
	public void deleteBindUserIdChannel() {
		String userId = String.valueOf(MaskIMSecurityUtils.getUserId());
		userIdChannel.remove(userId);
	}

	@Override
	public void deleteBindIdChannel(String id) {
		idChannel.remove(id);
	}

	@Override
	public void setUserIdAndIdBindChannel(String id, Channel channel) {
		setUserIdBindChannel(channel);
		setIdBindChannel(id,channel);
	}

	@Override
	public void deleteBindUserIdAndIdChannel(String id) {
		deleteBindIdChannel(id);
		deleteBindUserIdChannel();
	}

	@Override
	public Channel getUserIdChannel(String userId) {
		return userIdChannel.get(userId);
	}

	@Override
	public String getChannelUserId(Channel channel) {
		return ChannelUserId.get(channel);
	}

	@Override
	public List<Channel> getAllOnlineUser() {
		return new ArrayList<>(ChannelUserId.keySet());
	}

}
